package com.ruoyi.ora.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.dict.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ora.domain.*;
import com.ruoyi.ora.mapper.AssetsPermissionMapper;
import com.ruoyi.ora.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 资产权限Service业务层处理
 * 
 * @author liuwp
 * @date 2025-04-10
 */
@Service
public class AssetsPermissionServiceImpl implements IAssetsPermissionService 
{
    @Autowired
    private AssetsPermissionMapper assetsPermissionMapper;

    @Autowired
    private IVoiceRecordService voiceRecordService;
    @Autowired
    private IVoiceMemoirService voiceMemoirService;
    @Autowired
    private IVoiceStoryService voiceStoryService;
    @Autowired
    private IDeviceUserService deviceUserService;
    @Autowired
    private IUserMessageService userMessageService;


    /**
     * 查询资产权限
     * 
     * @param id 资产权限主键
     * @return 资产权限
     */
    @Override
    public AssetsPermission selectAssetsPermissionById(Long id)
    {
        return assetsPermissionMapper.selectAssetsPermissionById(id);
    }

    /**
     * 查询资产权限列表
     * 
     * @param assetsPermission 资产权限
     * @return 资产权限
     */
    @Override
    public List<AssetsPermission> selectAssetsPermissionList(AssetsPermission assetsPermission)
    {
        return assetsPermissionMapper.selectAssetsPermissionList(assetsPermission);
    }

    /**
     * 根据业务主键查询一条记录：资产权限
     *
     * @param assetsPermission 资产权限
     * @return 资产权限
     */
    @Override
    public AssetsPermission selectOneAssetsPermission(AssetsPermission assetsPermission)
    {
        return assetsPermissionMapper.selectOneAssetsPermission(assetsPermission);
    }

    /**
     * 新增资产权限
     * 
     * @param assetsPermission 资产权限
     * @return 结果
     */
    @Override
    public int insertAssetsPermission(AssetsPermission assetsPermission)
    {
        assetsPermission.setCreateTime(DateUtils.getNowDate());
        return assetsPermissionMapper.insertAssetsPermission(assetsPermission);
    }

    /**
     * 修改资产权限
     * 
     * @param assetsPermission 资产权限
     * @return 结果
     */
    @Override
    public int updateAssetsPermission(AssetsPermission assetsPermission)
    {
        return assetsPermissionMapper.updateAssetsPermission(assetsPermission);
    }

    @Override
    public int updateAssetsPermissionCondition(AssetsPermission assetsPermission, AssetsPermission assetsPermissionCondition)
    {
        return assetsPermissionMapper.updateAssetsPermissionCondition(assetsPermission, assetsPermissionCondition);
    }

    /**
     * 批量删除资产权限
     * 
     * @param ids 需要删除的资产权限主键
     * @return 结果
     */
    @Override
    public int deleteAssetsPermissionByIds(Long[] ids)
    {
        return assetsPermissionMapper.deleteAssetsPermissionByIds(ids);
    }

    /**
     * 删除资产权限信息
     * 
     * @param id 资产权限主键
     * @return 结果
     */
    @Override
    public int deleteAssetsPermissionById(Long id)
    {
        return assetsPermissionMapper.deleteAssetsPermissionById(id);
    }

    /**
     * 条件删除资产权限
     *
     * @param assetsPermission 资产权限
     * @return 结果
     */
    @Override
    public int deleteAssetsPermissionByCondition(AssetsPermission assetsPermission)
    {
        return assetsPermissionMapper.deleteAssetsPermissionByCondition(assetsPermission);
    }

    @Override
    public AjaxResult assetsPermissionGrant(AssetsPermission assetsPermission) {
        if (assetsPermission.getAssetsType() != null && assetsPermission.getAssetsId() != null && assetsPermission.getShowType() != null){
            if (assetsPermission.getAssetsType().intValue() == AssetsType.VOICE.getValue().intValue()){
                // 录音记录
                voiceRecordService.updateVoiceRecord(Builder.of(VoiceRecord::new)
                        .with(VoiceRecord::setId, assetsPermission.getAssetsId())
                        .with(VoiceRecord::setShowType, assetsPermission.getShowType())
                        .build());
            }else if (assetsPermission.getAssetsType().intValue() == AssetsType.MEMORI.getValue().intValue()){
                // 回忆录
                voiceMemoirService.updateVoiceMemoir(Builder.of(VoiceMemoir::new)
                        .with(VoiceMemoir::setId, assetsPermission.getAssetsId())
                        .with(VoiceMemoir::setShowType, assetsPermission.getShowType())
                        .build());
            }else if (assetsPermission.getAssetsType().intValue() == AssetsType.STROY.getValue().intValue()){
                // 故事
                voiceStoryService.updateVoiceStory(Builder.of(VoiceStory::new)
                        .with(VoiceStory::setId, assetsPermission.getAssetsId())
                        .with(VoiceStory::setShowType, assetsPermission.getShowType())
                        .build());
            }

            // 删除原权限
            this.deleteAssetsPermissionByCondition(Builder.of(AssetsPermission::new)
                    .with(AssetsPermission::setAssetsType, assetsPermission.getAssetsType())
                    .with(AssetsPermission::setAssetsId, assetsPermission.getAssetsId())
                    .build());

            if (assetsPermission.getShowType().intValue() == AssetsShowType.PARTABLE.getValue().intValue()){
                if (assetsPermission.getDeviceUserIds().length > 0){
                    // 插入新权限 因为家庭圈不会有太多人就循环了
                    for (Long deviceUserId : assetsPermission.getDeviceUserIds()) {
                        this.insertAssetsPermission(Builder.of(AssetsPermission::new)
                                .with(AssetsPermission::setAssetsType, assetsPermission.getAssetsType())
                                .with(AssetsPermission::setAssetsId, assetsPermission.getAssetsId())
                                .with(AssetsPermission::setDeviceUserId, deviceUserId)
                                .build());
                    }
                }else {
                    return AjaxResult.error(MessageUtils.message("ora.common.required"));
                }
            }

            if (assetsPermission.getAssetsType().intValue() == AssetsType.VOICE.getValue().intValue()){
                VoiceRecord voiceRecord = voiceRecordService.selectVoiceRecordById(assetsPermission.getAssetsId());
                if (voiceRecord != null && voiceRecord.getDeviceId() != null){

                    List<DeviceUser> deviceUsers = new ArrayList<>();
                    // 发送消息
                    if (assetsPermission.getShowType().intValue() == AssetsShowType.ALLABLE.getValue().intValue()){
                        // 获取所有设备用户
                        deviceUsers = deviceUserService.selectDeviceUserList(Builder.of(DeviceUser::new)
                                .with(DeviceUser::setDeviceId, voiceRecord.getDeviceId())
                                .with(DeviceUser::setStatus, DeviceUserStatus.BIND.getValue())
                                .with(DeviceUser::setDevicePermission, DevicePermission.MEMBER.getValue())
                                .build());
                    }else if (assetsPermission.getShowType().intValue() == AssetsShowType.PARTABLE.getValue().intValue()){
                        // 获取部分设备用户
                        for (Long deviceUserId : assetsPermission.getDeviceUserIds()) {
                            deviceUsers.add(deviceUserService.selectDeviceUserById(deviceUserId));
                        }
                    }

                    for (DeviceUser deviceUser : deviceUsers) {
                        userMessageService.insertUserMessage(Builder.of(UserMessage::new)
                                .with(UserMessage::setUserId, deviceUser.getUserId())
                                .with(UserMessage::setMsgType, UserMessageType.VOICE_UPLOAD.getValue())
                                .with(UserMessage::setAssetsId1, voiceRecord.getId().toString())
                                .with(UserMessage::setMsgTitle, "设备【"+deviceUser.getDeviceName()+"】有新的录音").build());
                    }

                }
            }


            return AjaxResult.success();
        }else {
            return AjaxResult.error(MessageUtils.message("ora.common.required"));
        }
    }

}
